var config = require('../../../config/config.js');
var goods_ids = [];
var goods_nums = [];
Page({
  data: {
    palce: 2,
    items: [
      { name: "wechat", value: "微信支付", id: 0, checked: true },
      { name: "offline", value: "货到付款", id: 1 },
    ],
    address_list: {},
    CartsData: [],
    goods_ids: goods_ids,
    goods_nums: goods_nums,
    buyer_name: '',
    mobile: '',
    buyer_address: '',
    url: config.order_pay,
    sid: 1,
    aid: 1,
  },

  resetParentArr: function (arr) {
    var pages = CartsData();
    var currPage = pages[pages.length - 1];   //当前页面
    var prevPage = pages[pages.length];  //下一个页面

    //直接调用xia一个页面的setData()方法，把数据存到下一个页面中去
    prevPage.setData({
      orderData: orderData
    });
    wx.setStorageSync("cart", CartsData);
  },

  //获取缓存渲染数据
  cartStroage: function (e) {
    var getCartData = wx.getStorageSync("cart");
    this.setData({
      CartsData: getCartData
    });

    var CartsData = this.data.CartsData;
    for (var j in CartsData) {
      if (CartsData[j]) {
        goods_ids.push(CartsData[j].id);
        goods_nums.push(CartsData[j].goods_num)
        this.setData({
          goods_ids: goods_ids,
          goods_nums: goods_nums
        })
      }
    }
    var total = 0;
    for (var i = 0; i < CartsData.length; i++) {
      if (!CartsData[i]) {
        continue;
      }
      if (CartsData[i].selected) {
        total += CartsData[i].goods_num * CartsData[i].goods_price;
      }
    }
    //TODO:根据缓存数据渲染页面
    this.setData({
      totalPrice: total.toFixed(2)
    })
  },


  radioChange: function (e) {
    var type = e.detail.value;
    if (type) {
      this.setData({
        url: config.order_add,
      })
    }
  },

  //结算请求
  intoTab: function (e) {
    var member_info = (wx.getStorageSync('member_info'));
    var mobile = parseInt(member_info.mobile)
    var that = this;
    if (!mobile) {
      wx.showModal({
        title: '提示',
        content: "你还未填写个人信息,请填写",
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '../../personal/personal',
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
      return
    }
    var type = 0;
    if (that.data.url !== config.order_pay) {
      type = 1;
    }
    wx.request({
      url: this.data.url,
      method: "POST",
      data: {
        type: type,
        aid: member_info.aid,
        mid: member_info.mid,
        sid: member_info.sid,
        goods_nums: this.data.goods_nums,
        goods_ids: this.data.goods_ids,
        order_amount: this.data.totalPrice,
        buyer_name: this.data.buyer_name,
        mobile: this.data.mobile,
        buyer_address: this.data.buyer_address,
        description: e.detail.value.description,
        openid: member_info.openid,
        formId: e.detail.formId
      },
      success: function (res) {
        if (!res.data.status) {
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel: false
          })
        } else {
          wx.setStorageSync("cart", []);
          if (that.data.url == config.order_pay) {
            that.Payment(res.data.data)
          } else {
            var ispaysuccess = 2;
            wx.redirectTo({
              url: 'pay-success/pay-success?ispaysuccess=' + ispaysuccess,
            })
          }
        }
      },
      fail: function (res) {
        console.log(res.data)
      }
    })
  },
  //微信支付
  Payment: function (data) {
    var ispaysuccess = 0;
    wx.requestPayment({
      'timeStamp': String(data.timeStamp),
      'nonceStr': data.nonceStr,
      'package': data.package,
      'signType': data.signType,
      'paySign': data.paySign,
      'success': function (res) {
        var ispaysuccess = 1;
        wx.redirectTo({
          url: 'pay-success/pay-success?ispaysuccess=' + ispaysuccess,
        })
      },
      'fail': function (res) {
        // wx.showToast({ title: '支付失败', icon: 'loading', duration: 20000 });
        wx.redirectTo({
          url: 'pay-success/pay-success?ispaysuccess=' + ispaysuccess,
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.cartStroage();
    this.getAddress();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */

  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
// 收货地址
 getAddress: function () {
    var that = this;
    var member_info = (wx.getStorageSync('member_info'));
    wx.request({
      url: config.address_index,
      method: "GET",
      data: {
      mid: member_info.mid,
      sid: member_info.sid,
      aid: member_info.aid,
      },
      success: function (res) {
        var result = res.data.data.info;
        if (result) {
          that.setData({
            place: 0,
            address_list: res.data.data.info,
            buyer_name: res.data.data.info.name,
            buyer_address: res.data.data.info.area_info + ' ' + res.data.data.info.address,
            mobile: res.data.data.info.mobile
          })
        }else {
          that.setData({
            place: 1
          })
        }
        if (!res.data.status) {
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel: false
          })
        }
      },
      fail: function (res) {
        console.log(res.data)
      }
    })
  },
  toAddress: function () {
    var reback = 0;
    wx: wx.navigateTo({
      url: '../../member/address/address?reback=' + reback,
    })
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getAddress();
    let pages = getCurrentPages();
    let currPage = pages[pages.length - 1];
    if (!currPage.data.address) {
      return
    }
    this.setData({//将携带的参数赋值
      address_list: currPage.data.address,
    });
    if (this.data.address_list) {
      this.setData({//将携带的参数赋值
        buyer_name: this.data.address_list.name,
        buyer_address: this.data.address_list.area_info + ' ' + this.data.address_list.address,
        mobile: this.data.address_list.mobile,
        palce: true,
      });
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})